<?php

/**
 * @author Alexia
 * @version 1.0
 */
class QuestionEvaluation
{

    /**
     * @var int
     */
    private $Id;
    /**
     * @var int
     */
    private $IdType;
    /**
     * @var string
     */
    private $Name;
    /**
     * @var string
     */
    private $Texte;

    /**
     * Constructeur
     * @param int $id
     */
    public function __construct($id = 0)
    {
        if (empty($id))
        {
            $this->Id = 0;
            $this->IdType = 0;
            $this->Name = "";
            $this->Texte = "";

            $this->requeteOk = true;
            $this->errormsg = "";
        }
        else
        {
            $sql = SQL::getInstance()->getConnection();
            //On retourne l'entree demandee
            $query = 'SELECT * FROM QuestionEvaluation WHERE idQuestionEvaluation = :id';
            $c = $sql->prepare($query);
            $c->bindParam(':id', $id, PDO::PARAM_INT);
            //Execution de la requête retourne false si echec
            if ($c->execute())
            {
                $result = $c->fetch(PDO::FETCH_OBJ);
                if (!empty($result))
                {
                    //ID
                    $this->Id = $result->idQuestionEvaluation;
                    $this->IdType = $result->IdTypeQuestion;
                    //String
                    $this->Name = $result->NameQuestion;
                    $this->Texte = $result->TexteQuestion;

                    $this->requeteOk = true;
                    $this->errormsg = "";
                }
                else
                {
                    $this->requeteOk = false;
                    $this->errormsg = "Cette question n'existe pas !";
                }
            }
            else
            {
                //Recuperation de l'erreur dans la variable de classe
                /*
                  Pour chaque new avec passage de paramètres vous pouvez savoir si vous avez bien un resultat, si false n'oubliez pas de gerez l'affichage du message,...
                 */
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
            }
        }
    }

    /**
     * Insertion d'une question
     */
    public function insertQuestionEvaluation()
    {
        $sql = SQL::getInstance()->getConnection();
        $query =
                'INSERT INTO QuestionEvaluation
		(
			 IdTypeQuestion,
			 NameQuestion,
			 TexteQuestion
		)
		VALUES
		(
			:idTQ,
			:name,
			:texte
		)';
        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);

            $c->bindParam(':idTQ', $this->IdType, PDO::PARAM_INT);

            $c->bindParam(':name', $this->Name, PDO::PARAM_STR);
            $c->bindParam(':texte', $this->Texte, PDO::PARAM_STR);

            if ($c->execute())
            {
                $this->Id = $sql->lastInsertId();
                $sql->commit();
                $c->closeCursor();
                $this->requeteOk = true;
                $this->errormsg = "Insertion reussie!";
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
                $this->Id = 0;
            }
        }
        catch (PDOException $Exception)
        {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
            $this->Id = 0;
        }
    }

    /**
     * Modification d'une question
     */
    public function updateQuestionEvaluation()
    {
        $sql = SQL::getInstance()->getConnection();
        $query =
                'UPDATE QuestionEvaluation SET
			 IdTypeQuestion      = :idTQ,
			 NameQuestion        = :name,
			 TexteQuestion       = :texte
		WHERE
			idQuestionEvaluation = :id
		';
        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);

            $c->bindParam(':id', $this->Id, PDO::PARAM_INT);
            $c->bindParam(':idTQ', $this->IdType, PDO::PARAM_INT);

            $c->bindParam(':name', $this->Name, PDO::PARAM_STR);
            $c->bindParam(':texte', $this->Texte, PDO::PARAM_STR);

            if ($c->execute())
            {
                $sql->commit();
                $c->closeCursor();
                $this->requeteOk = true;
                $this->errormsg = "Modification reussie!";
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
            }
        }
        catch (PDOException $Exception)
        {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
        }
    }

    /**
     * Supprime une Question de la base
     */
    public function deleteQuestionEvaluation()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = 'DELETE FROM QuestionEvaluation WHERE IdQuestionEvaluation = :id';
        $c = $sql->prepare($query);
        $c->bindParam(':id', $this->Id, PDO::PARAM_INT);
        $c->execute();
    }

    /**
     *
     * @return int
     */
    public function getIdQuestionEvaluation()
    {
        return $this->Id;
    }

    /**
     *
     * @return int
     */
    public function getIdTypeQuestion()
    {
        return $this->IdType;
    }

    /**
     * @param int $value
     * @return array
     */
    public function setIdTypeQuestion($value)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($value) && $value > 0)
        {
            $this->IdType = $value;
        }
        else
        {
            if (!isset($value))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'IdTypeQuestion ne peut etre null !";
            }
            if (isset($value) && $value <= 0)
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "L'IdTypeQuestion ne peut etre <= 0 !";
            }
            else
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Cet IdTypeQuestion n'est pas valide !";
            }

            $this->IdType = 0;
        }
        return $errTab;
    }

    /**
     *
     * @return string
     */
    public function getNameQuestion()
    {
        return $this->Name;
    }

    /**
     *
     * @param string $name
     * @return array
     */
    public function setNameQuestion($name)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($name) && $name != "" && strlen($name) <= 255)
        {
            $this->Name = htmlentities($name);
        }
        else
        {
            if (!isset($name))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Le nom ne peut etre null !";
            }
            if (isset($name) && $name == "")
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Le nom ne peut etre vide !";
            }
            if (isset($name) && strlen($name) > 255)
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Le nom ne peut etre aussi long !";
            }
            else
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Ce nom n'est pas valide !";
            }

            $this->Name = "";
        }
        return $errTab;
    }

    /**
     *
     * @return string
     */
    public function getTexteQuestion()
    {
        return $this->Texte;
    }

    /**
     *
     * @param string $text
     * @return array
     */
    public function setTexteQuestion($text)
    {
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        $errTab = array("boolOk" => true, "errmsg" => "");

        if (isset($text) && $text != "" && strlen($text) <= 255)
        {
            $this->Texte = htmlentities($text);
        }
        else
        {
            if (!isset($text))
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Le texte ne peut etre null !";
            }
            if (isset($text) && $text == "")
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Le texte ne peut etre vide !";
            }
            if (isset($text) && strlen($text) > 255)
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Le texte ne peut etre aussi long !";
            }
            else
            {
                $errTab["boolOk"] = false;
                $errTab["errmsg"] = "Ce texte n'est pas valide !";
            }

            $this->Texte = "";
        }
        return $errTab;
    }

}

?>